import cv2

# 读取原图和logo图
img = cv2.imread('../images/lena.png')
logo = cv2.imread('../images/hqyj_logo.png')
h, w, _ = logo.shape

# 对logo图进行灰度处理，二值化
logo_gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
ret, logo_binary = cv2.threshold(logo_gray, 70, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
cv2.imshow("logo binary", logo_binary)
cv2.waitKey(0)

# 在原图上截取和logo图一样大小的区域ROI，与logo_binary进行位运算
"""
    127 & 0 = 0
    127 & 255 = 127
"""
ROI = img[10:10+h, 10:10+w]
cv2.imshow("ROI", ROI)
cv2.waitKey(0)

ROI_bitadd_logo = cv2.bitwise_and(ROI,
                                  ROI,
                                  mask=logo_binary
                                  )
cv2.imshow("ROI_bitadd_logo", ROI_bitadd_logo)
cv2.waitKey(0)

# 将位与的结果彩底黑字的图像，与原logo【透明背景】进行加法运算
logo_add_ROI = cv2.add(ROI_bitadd_logo, logo)
cv2.imshow("logo_add_ROI", logo_add_ROI)
cv2.waitKey(0)

# 替换到图像上
img[10:10+h, 10:10+w] = logo_add_ROI
cv2.imshow("mosaic image", img)
cv2.waitKey(0)